# -*- coding: utf-8 -*-
"""
Created on Thu Dec  6 15:42:09 2018

@author: Rb87BEC
"""
from scipy import ndimage
from matplotlib import pyplot as plt
import numpy as np


def Count(gap, x, y, threshold, **kw):
    Gap_Min_Fil = ndimage.minimum_filter(gap, 3)
    Gap_Loc_Min = Gap_Min_Fil*(Gap_Min_Fil == gap)
    Gap_Loc_Min[Gap_Loc_Min > threshold] = 0
    Gap_Loc_Min[Gap_Loc_Min > 0] = 1
    Weyl_x = x[np.where(Gap_Loc_Min == 1)[0]]
    Weyl_y = y[np.where(Gap_Loc_Min == 1)[1]]
    Weyl_x[Weyl_x == 1] = -1
    Weyl_y[Weyl_y == 1] = -1
    use_list = np.asarray(np.abs(Weyl_x) > 1e-8).nonzero()
    Weyl_x = Weyl_x[use_list]
    Weyl_y = Weyl_y[use_list]
    use_list = np.asarray(np.abs(Weyl_x + 1) > 1e-8).nonzero()
    Weyl_x = Weyl_x[use_list]
    Weyl_y = Weyl_y[use_list]
    Weyl_Points = list(set(list(zip(Weyl_x, Weyl_y))))
    if 'z' in kw:
        z = kw['z']
        Weyl_z = z[np.where(Gap_Loc_Min == 1)[2]]
        Weyl_z[Weyl_z == 1] = -1
        Weyl_Points = list(set(list(zip(Weyl_x, Weyl_y, Weyl_z))))
    Number_of_WeylPoints = len(Weyl_Points)
    return Weyl_Points, Number_of_WeylPoints
